package com.uobgroup.ews.uam.core.domain;

import java.util.Calendar;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import flexjson.JSON;
import flexjson.JSONSerializer;

@MappedSuperclass
public abstract class BaseEntity {

   private String lastModificationUserId;

   @Temporal(TemporalType.TIMESTAMP)
   private Date lastModificationDate;

   @JSON(include = false)
   public String getLastModificationUserId() {
      return lastModificationUserId == null ? "" : lastModificationUserId;
   }

   public void setLastModificationUserId(String userId) {
      this.lastModificationUserId = userId;
   }

   @JSON(include = false)
   public Date getLastModificationDate() {
      lastModificationDate = Calendar.getInstance().getTime();
      return lastModificationDate;
   }

   public void setLastModificationDate(Date lastModificationDate) {
      this.lastModificationDate = lastModificationDate;
   }

   public abstract Long getId();

   @Override
   public String toString() {
      JSONSerializer serializer = new JSONSerializer();
      serializer.prettyPrint(true);
      String result = serializer.exclude("*.class").serialize(this);
      return result;
   }

   @Version
   @Column(name = "version")
   private Integer version;

   @JSON(include = false)
   public Integer getVersion() {
      return version;
   }

   public void setVersion(Integer version) {
      this.version = version;
   }

}
